//
//  ViewController.swift
//  L14_04_UIView2
//
//  Created by yao_yu on 14-10-6.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//

import UIKit

class YYButton: UIButton{
    class func create() -> UIButton{
        var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 10.0
        button.layer.borderWidth = 0.5


        button.layer.borderColor = UIColor.blackColor().CGColor
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        return button
    }
}

class ViewController: UIViewController {
    
    var lblOutput = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        
        setupViews()
    }
    
    //视图嵌套
    func setupViews(){
        
        lblOutput.frame = CGRectMake(10, 20, 300, 100)
        lblOutput.center = CGPointMake(view.center.x, view.center.y + 160)
        lblOutput.numberOfLines = 5
        view.addSubview(lblOutput)
        
        var button11 = YYButton.create()
//        button11.backgroundColor = UIColor.orangeColor()
        button11.frame = CGRectMake(10, 20, 300, 300)
        button11.center = CGPointMake(view.center.x, view.center.y - 40)
        button11.setTitle("1-1", forState: UIControlState.Normal)
        button11.addTarget(self, action: "onOK:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(button11)
        
        var button = YYButton.create()
        //button.backgroundColor = UIColor.yellowColor()
        button.frame = CGRectMake(20, 20, 260, 100)
        button.setTitle("1-1-1", forState: UIControlState.Normal)
        button.addTarget(self, action: "onOK:", forControlEvents: UIControlEvents.TouchUpInside)
        button11.addSubview(button)
        
        var button112 = YYButton.create()
        //button112.backgroundColor = UIColor.redColor()
        button112.frame = CGRectMake(20, 180, 260, 100)
        button112.setTitle("1-1-2", forState: UIControlState.Normal)
        button112.addTarget(self, action: "onOK:", forControlEvents: UIControlEvents.TouchUpInside)
        button11.addSubview(button112)
        
        button = YYButton.create()
        //button.backgroundColor = UIColor.greenColor()
        button.frame = CGRectMake(10, 10, 95, 80)
        button.setTitle("1-1-2-1", forState: UIControlState.Normal)
        button.addTarget(self, action: "onOK:", forControlEvents: UIControlEvents.TouchUpInside)
        button112.addSubview(button)
        
        button = YYButton.create()
        //button.backgroundColor = UIColor.greenColor()
        button.frame = CGRectMake(155, 10, 95, 80)
        button.setTitle("1-1-2-2", forState: UIControlState.Normal)
        button.addTarget(self, action: "onOK:", forControlEvents: UIControlEvents.TouchUpInside)
        button112.addSubview(button)
    }
    
    func onOK(button:UIButton){
        var output = "自己是 \(button.titleLabel!.text!)"
        if button.superview!.isKindOfClass(UIButton){
            output += "\n 父视图是 \((button.superview as UIButton).titleLabel!.text!)"
        } else {
            output += "\n 父视图是 ViewContoller"
        }
        var index = 0
        for subview in  button.subviews{
            if subview.isKindOfClass(UIButton){
                index += 1
                output += "\n 子视图\(index) = \((subview as UIButton).titleLabel!.text!)"
            }
        }
        lblOutput.text = output
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

